//
//  QCommonStateVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/20.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QCommonStateVC.h"
#import "QAddCommonCell.h"
#import "QInvoiceDetailModel.h"
#import "QInfoManageVC.h"
@interface QCommonStateVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSArray * titleArr;
@property (nonatomic,strong)QInvoiceDetailModel *detailModel;
@end

@implementation QCommonStateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"发票状态";
    _titleArr =[NSArray arrayWithObjects:@"发票类型", @"收票人姓名",@"收票人手机",@"收票人地址",nil];
    [self createUI];
    if (![_credentialsId isEqualToString:@""]&&_credentialsId!=nil) {
        [self requestData];
    }
}

- (void)requestData{
    NSString * url =[QParamService urlWithAction:@"findInvoiceDetail"];
    NSDictionary * para=[QParamService findInvoiceDetailWithCredentialsId:_credentialsId];
    [QViewUtility showMBHudToView:_tbView hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:_tbView];
        if (error) {
            return ;
        }
        QBaseModel * baseModel =[QBaseModel objectFromDict:dict];
        _detailModel = [QInvoiceDetailModel objectFromDict:baseModel.items];
        [_tbView reloadData];
        
    }];
}
- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo (@8);
        make.left.equalTo (@0);
        make.right.equalTo (@0);
        make.bottom.equalTo (@0);
    }];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QAddCommonCell" bundle:nil] forCellReuseIdentifier:@"QAddCommonCell"];
    
    UIView *footer =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
    
    UIButton * submitBtn = [QViewUtility createButtonWithFrame:CGRectMake(40, 40, kScreenWidth - 80, 45) font:Ksize17 title:@"提 交" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    submitBtn.layer.cornerRadius =7;
    submitBtn.layer.masksToBounds = YES;
    
    [submitBtn addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn blueStyle];
    submitBtn.hidden = YES;
    
    [footer addSubview:submitBtn];
    self.tbView.tableFooterView = footer;
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(bac)];

    if ([self.pushFromWhere isEqualToString:@"QAddCommonVC"]) {
        UIImageView *image =[[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth - 90, 20, 70, 70)];
        image.image = [UIImage imageNamed:@"invoice_pass"];
        [_tbView addSubview:image];
    }
    
//    UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    navButton.frame = CGRectMake(0,0,55, 45);
//    //navButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
//    navButton.titleLabel.font = [UIFont systemFontOfSize:16];
//    [navButton setTitle:@"修改" forState:UIControlStateNormal];
//    [navButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [navButton addTarget:self action:@selector(changeAction) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:navButton];
//    self.navigationItem.rightBarButtonItem = rightItem;


}
- (void)bac{
    if (_pop == NO) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        for (UIViewController *controller in self.navigationController.viewControllers){
            if ([controller isKindOfClass:[QInfoManageVC class]]){
                
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAddInvoiceSuccess object:nil];
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
        
    }
}

#pragma mark --- 提交
- (void)submitAction{
    
}
#pragma mark --- 修改
- (void)changeAction{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QAddCommonCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QAddCommonCell" forIndexPath:indexPath];
    cell.titleLab.text =_titleArr[indexPath.section];
    if (![_credentialsId isEqualToString:@""]&&_credentialsId!=nil) {
        if (indexPath.section == 0) {
            cell.invoiceTypeLab.text =_detailModel.invoiceName;
        }else if (indexPath.section == 1){
            cell.invoiceTypeLab.text =_detailModel.receiveName;
        }else if (indexPath.section ==2){
            cell.invoiceTypeLab.text =_detailModel.receivePhone;
        }else if (indexPath.section == 3){
            cell.invoiceTypeLab.text =_detailModel.receiveAddress;
        }

    }else{
        if (indexPath.section == 0) {
            cell.invoiceTypeLab.text =@"普票";
        }else if (indexPath.section == 1){
            cell.invoiceTypeLab.text =_model.receiveName;
        }else if (indexPath.section ==2){
            cell.invoiceTypeLab.text =_model.receivePhone;
        }else if (indexPath.section == 3){
            cell.invoiceTypeLab.text =_model.receiveAddress;
        }

    }
    return cell;
}


#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
